package org.jon.service.impl;

import org.jon.dao.UserDao;
import org.jon.domain.User;
import org.jon.service.UserService;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by administrator on 2017/8/20.
 */
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, readOnly = true)
public class UserServiceImpl implements UserService {
    private UserDao userDao;
    @Override
    public User getUserByCodePassword(User user) {
        // 1.根据用户名称查询登录用户
        User existU = userDao.getUserByUserCode(user.getUser_code());
        // 2.判断用户是否存在，不存在 --> 抛出异常，提示用户名不存在
        if (existU == null) {
            throw new RuntimeException("用户名不存在！");
        }
        // 3.判断用户密码是否正确  --> 不正确，抛出异常，提示密码错误
        if (!existU.getUser_password().equals(user.getUser_password())) {
            throw new RuntimeException("密码错误！");
        }
        // 4.返回查询到的用户对象
        return existU;
    }

    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, readOnly = false)
    public void saveUser(User user) {
        userDao.save(user);
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
}
